#pragma once

#include <iostream>
#include <atomic>
#include <vector>

#include "LuaDefine.h"

extern "C" {
//#include "lua.h"
//#include "lauxlib.h"
#include "../../lua-5.1.5/src/lualib.h"
}


using namespace std;

class LuaVM
{
public:
	LuaVM();
	~LuaVM();

private:
	lua_State* m_state;
	bool m_bInited;

public: 

	bool Init();
	bool Init(lua_State *pState);
	bool UnInit();

	bool ExecuteScriptFile(const char *FileName, void* pvOpenFile = nullptr);
	bool ExecuteScriptFromBuffer(const char *FileName, char* pBuffer, size_t nSize);

	void CallFunctionBegin(int *pIndex);
	void CallFunctionEnd(int nIndex);
	bool ExecuteString(const char *codes);

	bool PushFunction(const char* pcsFunction);
	bool CallFunction(int nArgs, int nResults);
public:

	lua_State* LuaState(void)
	{
		return m_state;
	}
};

lua_State* GetLuaState();
void Debug_Traceback();
bool g_CreateLuaVM(lua_State* pState);
bool g_CreateLuaVM();
LuaVM* g_GetLuaVM();
bool g_SetLuaVM(LuaVM* pLuaVM);
void g_DestroyLuaVM();